#!/usr/bin/python

import os
import sys
import urllib2
import feedparser
import usbmailnotifier
import ConfigParser

def main(argv):
  config = ConfigParser.RawConfigParser()
  config.read(os.path.expanduser('~/.gmailnotifier.conf'))

  gmail_username = config.get('options', 'gmail_username')
  gmail_password = config.get('options', 'gmail_password')

  auth = urllib2.HTTPBasicAuthHandler()
  auth.add_password('New mail feed', 'https://mail.google.com', gmail_username, gmail_password)

  opener = urllib2.build_opener(auth)
  feed_file = opener.open('https://mail.google.com/mail/feed/atom/')

  feed = feedparser.parse(feed_file)

  color = usbmailnotifier.USBMailNotifier.Color.RED
  if len(feed.entries) > 0:
    color = usbmailnotifier.USBMailNotifier.Color.GREEN

  usbMailNotifier = usbmailnotifier.USBMailNotifier()
  usbMailNotifier.open()
  usbMailNotifier.set_color(color)
  usbMailNotifier.close()

if __name__ == "__main__":
  main(sys.argv[1:])
